<?php
namespace app\zhihui\controller;

use app\common\model\ZhSupport as ZhSupportModel;
use app\common\model\Member as MemberModel;
use app\common\model\ZhSupportEducation as ZhSupportEducationModel;
use app\common\model\ZhSupportDoubt as ZhSupportDoubtModel;
use app\common\model\ZhSupportReply as ZhSupportReplyModel;
use app\common\model\Company as CompanyModel;
use app\home\controller\Base;

/**
 * 答疑解惑控制器
 * Class Zhsupport
 * @package app\index\controller
 */
class Zhsupport extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhSupportModel;
        $mmodel = new MemberModel;
        $emodel = new ZhSupportEducationModel;
        $dmodel = new ZhSupportDoubtModel;
        $rmodel = new ZhSupportReplyModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try {
            $map['is_share'] = $param['is_share'];
            $map['comid'] = $param['comid'];
            if(!empty($param['me'])){
                $map['mid'] = $user['id'];
            }else{
                if($param['is_share'] == 2){
                    $map['mid'] = $user['id'];
                }
            }
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size);
            foreach($lists as &$v){
                if($v['is_reply'] == 1){
                    $v['manager_img'] = config('admin.nouser_img');
                }
                if($v['is_niming'] == 1){
                    $v['username'] = '匿名';
                    $v['img'] = config('home.no_user_headimg');
                }else{
                    $member = $mmodel->findData($v['mid'],'id','username,img');
                    $http = substr($member['img'],0,4);
                    if($http != 'http'){
                        $v['img'] = config('domain_name').$member['img'];
                    }else{
                        $v['img'] = $member['img'];
                    }
                    $v['username'] = $member['username'];
                }
                $reply = $rmodel->findOne(['sid'=>$v['id']],'mid,content','id','desc');
                if(!empty($reply)){
                    $mem = $mmodel->findData($reply['mid'],'id','username');
                    $reply['username'] = $mem['username'];
                    $num = $rmodel->getAllCount(['sid'=>$v['id']]);
                    $reply['more_num'] = $num - 1;
                    $v['user_reply'] = $reply;
                }

//                $v['education_num'] = $emodel->getCount(['s.comid'=>$v['comid']]);
                $v['doubt_num'] = $dmodel->getCount(['s.comid'=>$v['comid'],'d.sid'=>$v['id']]);
                $v['support_education'] = $emodel->getAllCount(['sid'=>$v['id']]);
                $seducation = $emodel->findOne(['mid'=>$user['id'],'sid'=>$v['id']]);
                if(empty($seducation)){
                    $v['is_support_education'] = 2;
                }else{
                    $v['is_support_education'] = 1;
                }
                $doubt = $dmodel->findOne(['sid'=>$v['id'],'mid'=>$user['id']]);
                if(empty($doubt)){
                    $v['is_doubt'] = 2;
                }else{
                    $v['is_doubt'] = 1;
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [detial  详情]
     * @return json
     * @author [C。]
     */
    public function detial()
    {
        $param = input('param.');
        $model = new ZhSupportModel;
        $mmodel = new MemberModel;
        $cmodel = new CompanyModel;
        $rmodel = new ZhSupportReplyModel;
        $emodel = new ZhSupportEducationModel;
        $user = $this->user;
        try{
            $support = $model->findData($param['id'],'id','id,mid,comid,content,reply,create_time,update_time,is_niming');
            $mem = $mmodel->findData($support['mid'],'id','img,username');
            $company = $cmodel->findData($support['comid'],'id','compic');
            $http = substr($company['compic'],0,4);
            if($http != 'http'){
                $company['compic'] = config('domain_name').$company['compic'];
            }
            $support['compic'] = $company['compic'];

            if($support['is_niming'] == 1){
                $support['username'] = '匿名';
                $support['img'] = config('home.no_user_headimg');
            }else{
                $http = substr($mem['img'],0,4);
                if($http != 'http'){
                    $mem['img'] = config('domain_name').$mem['img'];
                }
                $support['username'] = $mem['username'];
                $support['img'] = $mem['img'];
            }

            $reply_num = $rmodel->getAllCount(['sid'=>$support['id']]);
            if(!empty($support['reply'])){
                $reply_num += 1;
            }
            $support['reply_num'] = $reply_num;
            $education_num = $emodel->getAllCount(['sid'=>$support['id']]);
            $support['education_num'] = $education_num;

            $education = $emodel->findOne(['sid'=>$support['id'],'mid'=>$user['id']]);
            if(empty($education)){
                $support['is_education'] = 2;
            }else{
                $support['is_education'] = 1;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',$support);
    }
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ZhSupportModel;
        $user = $this->user;
        try{
            $param['mid'] = $user['id'];
            $model->addData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
    /**
     * [addreply  师傅解惑]
     * @return json
     * @author [C。]
     */
    public function addreply()
    {
        $param = input('param.');
        $model = new ZhSupportModel;
        try{
            $data = array(
                'id' => $param['id'],
                'is_reply' => 1,
                'reply' => $param['reply']
            );
            $model->editData($data);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '解惑成功');
    }
}